package com.cloudz.iptv.background;

import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;

import java.io.IOException;
import java.net.URL;

/**
 * @File Name: com.cloudz.iptv.background
 * @Author: YJ //作者及
 * @Date: 2019/1/22 12:16 AM//完成日期
 * @Description: // 描述
 * @Version: v0.0.1 // 版本信息
 * @Function List: // 主要函数及其功能
 * @Others: // 其它内容的说明
 * @History: // 历史修改记录
 */
public class DownloadImageTask extends AsyncTask<String, Void, Drawable>
{
    public View view;

    public DownloadImageTask(View view)
    {
        this.view = view;
    }

    @Override
    protected void onPostExecute(Drawable drawable)
    {
        view.setBackgroundDrawable(drawable);
    }

    @Override
    protected Drawable doInBackground(String... urls)
    {
        return loadImageFromNetwork(urls[0]);
    }

    private Drawable loadImageFromNetwork(String imageUrl)
    {
        Drawable drawable = null;
        try
        {
            // 可以在这里通过第二个参数(文件名)来判断，是否本地有此图片
            drawable = Drawable.createFromStream(new URL(imageUrl).openStream(), null);
        } catch (IOException e)
        {
            Log.d("skythinking", e.getMessage());
        }
        if (drawable == null)
        {
            Log.d("skythinking", "null drawable");
        } else
        {
            Log.d("skythinking", "not null drawable");
        }

        return drawable;
    }


}
